<template>
  <el-card shadow="always" class="mt-10">
    <el-button
      type="primary"
      class="mb-20"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="addAttr"
    >
      添加属性
    </el-button>
    <el-table border :data="attrInfoList">
      <el-table-column label="序号" width="80px" type="index" align="center" />
      <el-table-column prop="attrName" width="150px" label="属性名称" />
      <el-table-column prop="name" width="831px" label="属性值列表">
        <template #default="scope">
          <el-tag
            type="success"
            v-for="attrValue in scope.row.attrValueList"
            :key="attrValue.id"
            class="mr-10"
          >
            {{ attrValue.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="150px" label="操作">
        <template v-slot="{ row }">
          <el-button
            class="mr-10"
            type="warning"
            size="small"
            :icon="Edit"
            title="修改属性"
            @click="updateAttr(row)"
          ></el-button>
          <el-popconfirm
            :title="`确定删除${row.attrName}吗？`"
            @confirm="deleteAttr(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                title="删除属性"
                :icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { watch, ref } from "vue";
import { Edit, Delete, Plus } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
import { useAttrInfo } from "@/stores/attr";
import { getAttrInfoList, deleteAttrApi } from "@/api/product/attr";
import type { AttrInfoList, AttrInfoItem } from "@/api/product/model/attrModel";
import { ElMessage } from "element-plus";
const categoryStore = useCategoryStore();
const attrInfoList = ref<AttrInfoList>([]);
// 接收自定义事件
const emit = defineEmits(["updateIsShowAttrList"]);
// 封装公共请求的函数
const getAttrInfo = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;
  attrInfoList.value = await getAttrInfoList({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};
// 更新属性
const attrInfo = useAttrInfo();
const updateAttr = (row: AttrInfoItem) => {
  emit("updateIsShowAttrList", true);
  attrInfo.attrInfo = row;
};
// 添加属性
const addAttr = () => {
  emit("updateIsShowAttrList", true);
  // 重置为初始值
  attrInfo.$reset();
};
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      attrInfoList.value = [];
      return;
    }
    getAttrInfo();
  },
  { immediate: true }
);
// 删除属性
const deleteAttr = async (id: number) => {
  await deleteAttrApi(id);
  ElMessage.success("删除成功");
  getAttrInfo();
};
</script>

<style scoped></style>
